<?php

/**
 * This is the model class for table "{{members_refund}}".
 *
 * The followings are the available columns in table '{{members_refund}}':
 * @property integer $id
 * @property integer $uid
 * @property string $username
 * @property string $money
 * @property integer $status
 * @property string $remark
 */
class MembersWithdrawalModel extends BaseModel
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{members_withdrawal}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('uid, status', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>50),
			array('money', 'length', 'max'=>10),
			array('remark', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('add_time,username,remark', 'safe'),
			array('id, uid, username, money, status, remark', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'uid' => 'Uid',
			'username' => '用户名',
			'money' => '提现金额',
			'status' => '状态',
			'remark' => '备注',
			'add_time' => '提交时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('money',$this->money,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('remark',$this->remark,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function createSearchCriteria(){
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('money',$this->money,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('remark',$this->remark,true);
		return 	$criteria;	
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return MembersRefundModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * 判断某用户是否可提现
	 * 如果此用户还有正在或将来审核通过后上架的商品，则不能提现
	 * $money提现金额
	 * @return [type] [description]
	 */
	public  function canWithdrawal($money, $userInfo = ''){
		$time = time();
		empty($userInfo) ? $userInfo = yii::app()->user->getInfo() : '';
		if($userInfo['gold'] < $money){
			$this->addError('', '用户金额不足');
			return false;
		}		
		$count = GoodsBmModel::model()->count("end_time > $time AND pay_status = 2 AND status not in(2,4)");
		if($count > 0){
			$this->addError('', '检测到你还有商品正在上架,还不能提现');
			return false;			
		}
		return true;
	}

	public function beforeSave(){
		if($this->isNewRecord){
			$this->status = 0;
			$this->add_time = time();
		}
		return true;
	}

	public static function getStatusHtmlList()
	{
		return array(
			0 => '未处理',
			1 => '已处理',
			2 => '已取消',
		);
	}

	public static function getStatusHtmlDetail($data)
	{
		$arrData = self::getStatusHtmlList();
		return  $arrData[$data];
	}

}
